extends State
var animated_sprite_2d: AnimatedSprite2D

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.
	
func Enter() -> void:
	# 1. 类型断言：确保 actor 是我们需要的 CharacterBody2D 类型
	assert(actor is CharacterBody2D, "Idle state requires a CharacterBody2D actor.")
	
	# 2. 安全地获取 actor 上的组件引用
	# player.gd 脚本上有一个 animated_sprite_2d 的导出变量，我们可以直接获取
	animated_sprite_2d = actor.animated_sprite_2d
	assert(animated_sprite_2d != null, "Actor is missing the AnimatedSprite2D reference.")

func Physic_Update(_delta:float) -> void:
	var direction = Input.get_axis(&"Left",&"Right")
	var player_actor := actor as CharacterBody2D
	if Input.get_axis(&"Left",&"Right") == 0:
		Transitioned.emit(self.name, "idle")
	else:
		player_actor.velocity.x = 200 * direction
		animated_sprite_2d.play("running")
		animated_sprite_2d.flip_h = Input.get_axis(&"Left",&"Right") < 0
		player_actor.move_and_slide()
